/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.dialog.grade;

import kz.kstu.ent.app.delegate.api.GradeDelegate;
import kz.kstu.ent.app.delegate.api.StudentDelegate;
import kz.kstu.ent.app.delegate.impl.GradeDelegateImpl;
import kz.kstu.ent.app.delegate.impl.StudentDelegateImpl;
import kz.kstu.ent.business.entity.Grade;
import kz.kstu.ent.business.entity.Student;
import kz.kstu.ent.desktop.components.model.AbstractNumeratedTableModel;
import kz.kstu.ent.desktop.images.GUIFactory;
import kz.kstu.ent.desktop.images.GUIIcon;

import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Date: 06.10.12
 * Time: 17:13
 *
 * @author Oleg Anastassov
 */
public class AllGradesDialog extends JDialog {

    private static final String TITLE = "Лицей :: Список классов";

    private static final int WIDTH = 900;
    private static final int HEIGHT = 600;

    private JTable allGradesTable;
    private JButton closeButton;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
    private JPanel rootPanel;

    private AllGradesTableModel tableModel;

    public AllGradesDialog() {
        setIconImage(
                GUIFactory.getImage(GUIIcon.STUDENTS)
        );
        tableModel = new AllGradesTableModel();
    }


    public void initStaticComponents() {
        setContentPane(rootPanel);
        setModal(true);
        setSize(WIDTH, HEIGHT);
        allGradesTable.setModel(getTableModel());
        setResizable(false);
        allGradesTable.getTableHeader().setReorderingAllowed(false);
        allGradesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        TableColumnModel columnModel = allGradesTable.getColumnModel();
        TableColumn idColumn = columnModel.getColumn(AbstractNumeratedTableModel.ID_COLUMN_INDEX);
        idColumn.setMinWidth(0);
        idColumn.setMaxWidth(0);
        setTitle(TITLE);

        initIcons();
        initListeners();
    }

    public void initData() {
        GradeDelegate delegate = new GradeDelegateImpl();
        List<Grade> grades = delegate.findAll(Grade.class);
        tableModel.setData(grades);
    }

    private AbstractNumeratedTableModel getTableModel() {
        tableModel = new AllGradesTableModel();
        return tableModel;
    }

    private void initListeners() {

        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private void initIcons() {
        addButton.setIcon(
                GUIFactory.getIcon(GUIIcon.STUDENT_ADD)
        );
        editButton.setIcon(
                GUIFactory.getIcon(GUIIcon.STUDENT_EDIT)
        );
        deleteButton.setIcon(
                GUIFactory.getIcon(GUIIcon.STUDENT_DELETE)
        );
        closeButton.setIcon(
                GUIFactory.getIcon(GUIIcon.CANCEL)
        );
    }


    private Student getSelectedObject() {
        int index = allGradesTable.getSelectedRow();
        if (index == -1) {
            return null;
        }
        Long id = (Long) tableModel.getValueAt(index, AbstractNumeratedTableModel.ID_COLUMN_INDEX);
        StudentDelegate delegate = new StudentDelegateImpl();
        return delegate.findById(Student.class, id);
    }

    public JPanel getRootPanel() {
        return rootPanel;
    }

}
